<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="12">
            <a-form-item label="项目简称" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['projectName']"
                placeholder="请选择项目简称"
                @click="projectShortNameClick"
              ></a-input>
              <a-input v-decorator="['projectId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['projectCode']" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目经理" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['charger']"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="项目专题" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input
                v-decorator="['subjectName']"
                placeholder="请选择专题名称"
                @click="subjectNameClick"
              ></a-input>
              <a-input v-decorator="['subjectId']" v-show='false'></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="专题编号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['subjectCode']"/>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="风险类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['riskType']" :trigger-change="true" dictCode="risk_type"  />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="风险等级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['riskGrade']" :trigger-change="true" dictCode="risk_grade"  />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['riskState']"  :trigger-change="true" dictCode="icm_pm_projectRisks"  />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="负责人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false"v-decorator="['dutyPersion']"
              />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="确认人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false" v-decorator="['confirmorName']"></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="确认时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date v-decorator="['confirmorTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="创建人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-user-by-dep :multi="false" v-decorator="['createBy']"></j-select-user-by-dep>
            </a-form-item>
          </a-col>
          <a-col :span="12">
            <a-form-item label="创建时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date v-decorator="['createTime']" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="风险描述" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['riskDes',validatorRules.riskDes]"  ></a-textarea>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="影响分析" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-textarea v-decorator="['affect']" rows="4" placeholder="请输入影响分析" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="应对措施" :labelCol="labelCol2" :wrapperCol="wrapperCol2">
              <a-input v-decorator="['solutions']" placeholder="请输入应对措施"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
    <hf-project-select-modal ref="hfProjectSelectModal" :multiple="false" @hf-pm-project-select="hfPmProjectSelect"/>
    <hf-subject-select-modal ref="hfSubjectSelectModal" :multiple="false" @hf-subject-select="hfSubjectSelect"/>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import HfProjectSelectModal from '@views/hifar/pm/modules/HfProjectSelectModal'
  import HfSubjectSelectModal from '@views/hifar/pm/modules/HfSubjectSelectModal'


  export default {
    name: 'HfPmProjectRisksForm',
    components: {
      HfProjectSelectModal,
      HfSubjectSelectModal
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        labelCol2: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol2: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        confirmLoading: false,
        validatorRules: {
        },
        url: {
          add: "/pm/hfPmProjectRisks/add",
          edit: "/pm/hfPmProjectRisks/edit",
          queryById: "/pm/hfPmProjectRisks/queryById"
        },
        fields: [
          'createBy','createTime','deleteFlag','remarks','riskCode','projectId','projectCode',
          'projectName','charger','riskType','riskGrade','riskState','riskDes','affect','solutions','dutyPersion','confirmorId','confirmorDes',
          'confirmorName','confirmorTime','subjectId','subjectName','subjectCode'
        ]
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'createBy','createTime','deleteFlag','remarks','riskCode','projectId','projectCode',
            'projectName','charger','riskType','riskGrade','riskState','riskDes','affect','solutions','dutyPersion','confirmorId','confirmorDes',
            'confirmorName','confirmorTime','subjectId','subjectName','subjectCode'
          ))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            console.log("表单提交数据",formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'createBy','createTime','deleteFlag','remarks','riskCode','projectId','projectCode',
          'projectName','charger','riskType','riskGrade','riskState','riskDes','affect','solutions','dutyPersion','confirmorId','confirmorDes',
          'confirmorName','confirmorTime','subjectId','subjectName','subjectCode'))
      },
      projectShortNameClick() {
        this.$nextTick(() => {
          this.$refs.hfProjectSelectModal.title = '选择项目'
          this.$refs.hfProjectSelectModal.visible = true
        })
      },
      hfPmProjectSelect(data) {
        this.form.setFieldsValue({
          projectId: data.id,
          projectName: data.shortName,
          projectCode: data.code
        })
      },
      subjectNameClick() {
        this.$nextTick(() => {
          this.$refs.hfSubjectSelectModal.title = '选择主题'
          this.$refs.hfSubjectSelectModal.visible = true
        })
      },
      hfSubjectSelect(data) {
        this.form.setFieldsValue({
          subjectId: data.id,
          subjectName: data.subjectName,
          subjectCode:data.subjectCode
        })
      }
    }
  }
</script>